﻿using CPPEI.Coolzon.Metadata;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.DataProvider.Cache
{
    class DataSourceIndex
    {
        private List<DataSourceCache> _dataSourceCacheList;
        private object _lock = new object();

        public DataSourceIndex()
        {
            _dataSourceCacheList = new List<DataSourceCache>();
        }

        public DataSourceCache Get(string dataSourceName)
        {
            for (int index = 0; index < _dataSourceCacheList.Count; index++)
            {
                if (_dataSourceCacheList[index].Name == dataSourceName)
                {
                    return _dataSourceCacheList[index];
                }
            }
            return null;
        }

        public DataSourceCache Store(string dataSourceName)
        {
            DataSourceCache cache = this.Get(dataSourceName);
            if (cache == null)
            {
                lock (_lock)
                {
                    cache = this.Get(dataSourceName);
                    if (cache == null)
                    {
                        cache = new DataSourceCache(dataSourceName);
                        _dataSourceCacheList.Add(cache);
                        return cache;
                    }
                }
            }
            return cache;
        }
    }
}
